package com.witdata.controller.community;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.CommunityMemberDto;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.service.community.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/community/member")
@RequiredArgsConstructor
public class MemberController {
    private final MemberService memberService;

    @GetMapping("/state")
    public Result<Long> getState(@RequestParam("id") Long id) {
        long state = memberService.getState(id);
        return Result.ok(Status.SUCCESS, state);
    }

    @GetMapping("/check")
    public Result<Boolean> checkWhetherToJoin(@RequestParam("id") Long id) {
        boolean checked = memberService.checkWhetherToJoin(id);
        return Result.ok(Status.SUCCESS, checked);
    }

    @PostMapping("/join")
    public Result<String> joinCommunity(@RequestBody CommunityMemberDto communityMemberDto) {
        if (ObjectUtils.isEmpty(communityMemberDto.getCommunityId())
                || ObjectUtils.isEmpty(communityMemberDto.getUserId())
        ) throw new GlobalException(Status.PARAMERROR);
        memberService.joinCommunity(communityMemberDto);
        return Result.ok(Status.JoinSuccess);
    }

    @PostMapping("/leave")
    public Result<String> leaveCommunity(@RequestBody CommunityMemberDto communityMemberDto) {
        if (ObjectUtils.isEmpty(communityMemberDto.getCommunityId())
                || ObjectUtils.isEmpty(communityMemberDto.getUserId())
        ) throw new GlobalException(Status.PARAMERROR);
        memberService.leaveCommunity(communityMemberDto);
        return Result.ok(Status.LeaveSuccess);
    }
}
